//
//  CalculatorViewController.m
//  Calculator
//
//  Created by Yingyu on 11-5-31.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CalculatorViewController.h"

@implementation CalculatorViewController

@synthesize display;
@synthesize showFoperator;

- (void)viewDidLoad 
{
	display.text = @"";
	showFoperator.text = @"";
	operator = @"=";
	fstOperand = 0;
	sumOperand = 0;
	bBegin = YES;

}

-(IBAction)buttonClicked:(id)sender 
{
	UIButton *btn = (UIButton *)sender;
	int tag = btn.tag;	
	switch (tag) 
	{
		case clearBtn:	// C    1
			[self clearDisplay];
			break;
		case plusBtn:	// +    3
		case subBtn:	// -    4
		case mulBtn:	// x    5
		case divBtn:	// ÷    6
		case equalBtn:	// =    9
			[self inputDoubleOperator:btn.titleLabel.text];
			break;
		case dotBtn:	// .    10
			[self addDot];
			break;
		case signBtn:	// +/-  11
			[self addSign];
			break;            
		default:
			[self inputNumber:btn.titleLabel.text];
			break;
	}
}

- (void)clearDisplay
{
	display.text = @"";
	showFoperator.text = @"0";
	operator = @"=";
	fstOperand = 0;
	sumOperand = 0;
	bBegin = YES;

}


- (void)inputDoubleOperator: (NSString *)dbopt
{
	showFoperator.text = dbopt;	
	if(![display.text isEqualToString:@""])
	{
		fstOperand = [display.text doubleValue];
		
		if(bBegin)
		{
			operator = dbopt;
		}
		else
		{           
			if([operator isEqualToString:@"="])
			{
				sumOperand = fstOperand;
			}
			else if([operator isEqualToString:@"+"])
			{
				sumOperand += fstOperand;
				display.text = [NSString stringWithFormat:@"%g",sumOperand];
			}
			else if([operator isEqualToString:@"-"])
			{
				sumOperand -= fstOperand;
				display.text = [NSString stringWithFormat:@"%g",sumOperand];
			}
			else if([operator isEqualToString:@"x"])
			{
				sumOperand *= fstOperand;
				display.text = [NSString stringWithFormat:@"%g",sumOperand];
			}
			else if([operator isEqualToString:@"÷"])
			{
				if(fstOperand!= 0)
				{
					sumOperand /= fstOperand;
					display.text = [NSString stringWithFormat:@"%g",sumOperand];
				}
				else
				{
					display.text = @"nan";
					operator= @"=";
				}
			}
			
			bBegin= YES;
			operator= dbopt;
		}
	}
}

- (void)addDot
{
	showFoperator.text = @".";
	if(![display.text isEqualToString:@""] && ![display.text isEqualToString:@"-"])
	{
		NSString *currentStr = display.text;
		BOOL notDot = ([display.text rangeOfString:@"."].location == NSNotFound);
		if (notDot) 
		{
			currentStr= [currentStr stringByAppendingString:@"."];
			display.text= currentStr;
		}
	}
}

- (void)addSign
{
	showFoperator.text = @"+/-";	
	if(![display.text isEqualToString:@""] && ![display.text isEqualToString:@"0"] && ![display.text isEqualToString:@"-"])
	{
		double number = [display.text doubleValue];
		number = number*(-1);
		display.text= [NSString stringWithFormat:@"%g",number];
		
		if(bBegin)
		{
			sumOperand = number;
		}
	}    
}


- (void)inputNumber: (NSString *)nbstr
{
	if(bBegin)
	{
		showFoperator.text = @"";
		display.text = nbstr;
	}
	else
	{
		display.text = [display.text stringByAppendingString:nbstr];
	}
	bBegin = NO;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload
{
	self.display = nil;
	self.showFoperator = nil;

}

- (void)dealloc
{
	[display release];
	[showFoperator release];
    [super dealloc];
}

@end
